-- kill_summon
-- created by xinj
-- 击杀敌人概率召唤

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,
    -- 触发
    trigger = function(who, prop, targetPos)
        if who.type ~= OBJECT_TYPE_USER then
            return;
        end

        -- 不满足概率
        local rand = DungeonM.getRandSeed("kill_summon");
        if rand % 1000 >= prop[3] then
            return;
        end

        -- 如果此时怪物全清了
        if DungeonServiceM.isMonsterClear() then
            DungeonLogM.addLog("怪物全清了，不再召唤出召唤物。");
            return;
        end

        local element = tonumber(prop[2]);
        local class = DungeonM.queryElement(element, "class");
        local dbase = DungeonM.queryElement(element, "dbase");
        local arr = table.deepcopy(dbase["summons"]);
        local pos = SummonM.findSummonPos(targetPos);

        if pos < 0 then
            DungeonLogM.addLog("地图全满了，不再召唤出召唤物。");
            return;
        end

        table.sort(arr);
        local classId = arr[rand % #arr + 1];

        local msg = string.format("召唤%d。", classId);
        DungeonLogM.addLog(msg);

        local para = {
            ["class"]   = class,
            ["element"] = element,
            ["layer"]   = DungeonM.currentLayer(),
            ["pos"]     = pos,
            ["active_round"] = CombatM.getRound(),
        };

        -- 召唤
        SummonM.doSummon(classId, para);

        local grid = DungeonM.getGridByPos(pos);
        local summon = grid.summon;
        if summon then
            if type(dbase["user_attack_rate"]) == "number" then
                -- 用玩家的攻击按比例初始化
                local attack = math.modf(dbase["user_attack_rate"] * who:queryAttrib("attack") / 1000);
                summon.dbase:set("attack", attack);
            end

            if type(dbase["disappear"]) == "number" then
                summon.dbase:set("disappear", dbase["disappear"]);
            end
         end
    end,

    record = function(target, prop, path)
        return false;
    end
};
